<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ShopController extends Controller
{
    //店铺添加
    public function add(Request $request)
    {
    	$title = '商铺添加';

      //会话跟踪
      $staff = $request->session()->get('staff');

    	//返回视图
    	return view('admin/shop/add',['title' => $title,'staff' => $staff]);
    }

    //执行商铺添加
    public function insert(Request $request)
    {
    	// dd($request -> all());
    	 //获取添加页面提交上来的数据,并去除了'_token'隐藏的数据
       $data = $request -> except('_token');
       //检测添加页面的数据并执行响应
       $this -> validate($request,[
            'shopnum' => 'required',
            'shopname' => 'required',
            'dzname' => 'required',
            'dzphone' => 'required|max:11|min:11|regex:[1[0-9]{10}]',
            'qq' => 'required',
            'seller_id' => 'required',
            'logo' => 'required',

        ],[

            'shopname.required' => '商铺名不能为空',
            'shopnum.required' => '商铺编号不能为空',
            'dzname.required' => '店主名不能为空',
            'dzphone.required' => '店主手机不能为空',
            'dzphone.max' => '请输入正确的手机号码',
            'dzphone.min' => '请输入正确的手机号码',
            'dzphone.regex' => '请输入正确的手机号码',
            'logo.required' => '店铺logo不能为空',
            'qq.required' => '商铺qq不能为空',
            'seller_id.required' => '卖家ID不能为空',
            
        ]);
       // 验证商铺名是否已经存在
       $res = DB::table('bro_shop') -> where('shopname',$data['shopname']) -> get();
       // dd($res);
       if(!empty($res))
       {
        return back() -> with('oldshopname','商铺名称已存在');
       }
       //处理logo地址,存入数据库
       
       if($request -> hasFile('logo'))//判断是否有文件
       {
            if($request -> file('logo') -> isValid())//判断文件是否有效
            {
                //获取文件的后缀名
                $suffix = $request -> file('logo') -> getClientOriginalExtension();
                // echo $suffix;
                //制作文件名
                $fileName = time().rand().'.'.$suffix;
                //上传文件
                $path = './uploads/shop';//上传文件的目录

                //执行上传
                $request -> file('logo') -> move($path,$fileName);
                //拼凑pic的URL地址
                $data['logo'] = trim($path.'/'.$fileName,'.');
                // return $data['pic'];
                
            }

       }

       //将数据发送到数据库
       $res = DB::table('bro_shop') -> insert($data);

       //执行发送判断
       if($res)
       {
         return redirect('/admin/shop/index') -> with('success','用户添加成功');
       }else{
         return back() -> with('error','用户添加失败');
       }
    }

    //商铺列表
    public function index(Request $request)
    {
    	$title = "商铺列表";


      //会话跟踪
      $staff = $request->session()->get('staff');

      //执行商铺数据的查询
    	 $data = DB::table('bro_shop') ->where(function($query) use ($request){
            $query -> where('shopname','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',10));

        //向用户列表发送数据
        return view('admin.shop.index',['title' => $title,'data' => $data,'request' => $request -> all(),'staff' => $staff]);
    }

    //商铺编辑
    public function edit(Request $request,$id)
    {
      $title= '用户编辑';


      //会话跟踪
      $staff = $request->session()->get('staff');
      //执行数据查询
      $data = DB::table('bro_shop') -> where('id',$id) -> first();
      //解析模板
      return view('/admin/shop/edit',['title'=>$title,'data'=>$data,'staff' => $staff]);
    	

    }

    public function update(Request $request)
    { 
      //获取添加页面提交上来的数据,并去除了'_token'隐藏的数据
       $data = $request -> except('_token','oldlogo');
       //检测添加页面的数据并执行响应
       $this -> validate($request,[
            'shopname' => 'required',
            'dzname' => 'required',
            'dzphone' => 'required',
            'qq' => 'required',
            'seller_id' => 'required',

        ],[

            'shopname.required' => '商铺名不能为空',
            'dzname.required' => '店主名不能为空',
            'dzphone.required' => '店主手机不能为空',
            'qq.required' => '商铺qq不能为空',
            'seller_id.required' => '卖家ID不能为空',
            
        ]);
       
       //处理logo地址,存入数据库
       
       if($request -> hasFile('logo'))//判断是否有文件
       {
            if($request -> file('logo') -> isValid())//判断文件是否有效
            {
                //获取文件的后缀名
                $suffix = $request -> file('logo') -> getClientOriginalExtension();
                // echo $suffix;
                //制作文件名
                $fileName = time().rand().'.'.$suffix;
                //上传文件
                $path = './uploads/shop';//上传文件的目录

                //执行上传
                $request -> file('logo') -> move($path,$fileName);
                //拼凑pic的URL地址
                $data['logo'] = trim($path.'/'.$fileName,'.');
                // return $data['pic'];
                $oldpath = '.'.$request -> input('oldlogo');
                // dd($oldpath);
                //删除以前的商铺logo
                if($oldpath != '.')
                {
                  if(file_exists($oldpath))
                  {  
                      unlink($oldpath);
                  }
                }

            }
       }

       //执行修改操作
       $res = DB::table('bro_shop') -> where('id',$data['id']) -> update($data);

       //判断修改是否成功
       if($res)
       {
         return redirect('/admin/shop/index') -> with('success','商铺修改成功');
       }else{
         return back() -> with('error','商铺修改失败');
       }
    }

    //删除商铺
    public function delete($id)
    {
       // dd($id);
       // 删除商铺的logo
       $logo = DB::table('bro_shop')-> where('id',$id) -> first() -> logo;
        $oldpath = '.'.$logo;
                
        if(file_exists($oldpath))
        {
            
              unlink($oldpath);
            
            
        }
        // dd($pic);
        $res = DB::table('bro_shop')-> where('id',$id) -> delete();

        //判断删除是否成功
       if($res)
        {
            return redirect('/admin/shop/index') -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }

}
